﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubRotate : MonoBehaviour
{
        [Header("旋转速度")]
        public float speed = 34f;

        [Header("顺时针旋转")]
        public bool isClockWise = false;

        [Header("绕X轴旋转")]
        public bool isRotateX = false;
        [Header("绕Y轴旋转")]
        public bool isRotateY = true;
        [Header("绕Z轴旋转")]
        public bool isRotateZ = false;
        [Header("旋转等待时间")]
        public int waitTime = 3;

        public bool isUseAuto;

        //真实的速度
        float curSpeed = 0;

        void Start()
        {
                isUseAuto = true;
                fwd = this.transform.forward;
                fwd.Normalize();
        }

        void FixedUpdate()
        {
                if (!isUseAuto)
                {
                        curSpeed = 0;
                }

                if (isUseAuto)
                {
                        do
                        {
                                if (curSpeed > speed)
                                {
                                        curSpeed -= 4;
                                }
                                if (curSpeed < speed)
                                {
                                        curSpeed += 4;
                                }
                        } while (false);

                        float trueSpeed = 1 * Time.deltaTime * curSpeed;

                        //顺逆时针处理
                        if (!isClockWise)
                        {
                                trueSpeed *= -1;
                        }

                        //X方向旋转
                        if (isRotateX)
                        {
                                this.transform.Rotate(new Vector3(trueSpeed, 0, 0));
                        }
                        //Y方向旋转
                        if (isRotateY)
                        {
                                this.transform.Rotate(new Vector3(0, trueSpeed, 0));
                        }
                        //Z方向旋转
                        if (isRotateZ)
                        {
                                this.transform.Rotate(new Vector3(0, 0, trueSpeed));
                        }
                }
        }

        void Update()
        {
                if (!gameObject.activeSelf)
                {
                        isUseAuto = true;
                }
                DelayUseAuto();
        }

        //关闭自动的请求
        private bool closeAutoFlag = false;
        //累加的X，Y值
        //Vector2 mun;
        //初始化存的正方向
        Vector3 fwd ;
        /// <summary>
        /// 旋转函数
        /// </summary>
        /// <param name="v">旋转的增量</param>
        /// <param name="isOne">旋转的算法</param>
        internal void TouchRotate(Vector2 v,bool isOne)
        {
                closeAutoFlag = true;
                Debug.Log(v);
                //旋转角度
                /*
                if (isOne)
                {
                        //欧拉不适合自动旋转
                        mun.x -= v.x * speed * 0.02f;
                        mun.y += v.y * speed * 0.02f;
                        //欧拉角转化为四元数
                        Quaternion rotation = Quaternion.Euler(mun.y, mun.x, 0);
                        this.transform.rotation = rotation;
                }
                else
                {
                 */
                Vector3 vaxis = Vector3.Cross(fwd, Vector3.right);
                transform.Rotate(vaxis, -v.x * speed * 0.02f, Space.World);
                Vector3 haxis = Vector3.Cross(fwd, Vector3.up);
                transform.Rotate(haxis, -v.y * speed * 0.02f, Space.World);
        }
        internal void TouchRotate(Vector2 v)
        {
                TouchRotate(v, false);
        }
        private long startTime;
        /// <summary>
        /// 延时用自动
        /// </summary>
        void DelayUseAuto()
        {
                if (!isUseAuto)
                {
                        Debug.Log("检测时间");
                        long nowTime = CubMinuyTool.ConvertDateTimeToLong(System.DateTime.Now);
                        if ((nowTime - startTime) > waitTime)
                        {
                                Debug.Log("超时");
                                isUseAuto = true;
                        }
                }

                if (closeAutoFlag)
                {
                        Debug.Log("收到停止自传请求，更新时间戳！");
                        startTime = CubMinuyTool.ConvertDateTimeToLong(System.DateTime.Now);
                        closeAutoFlag = false;
                        isUseAuto = false;
                }
        }
}
